<template>
	<view class="loginn">
		<topTitle :styles="{back:false,title:'登录',color:'transparent',bgColor:'none'}" />
		<view class="login">
			<view class="top_logo">
				<image :src="logo" mode="widthFix"></image>
			</view>
			<!-- <view class="title">
				欢迎登录亨享到家服务平台
			</view> -->
			<view class="login_button" @click="login">
				<!-- <uni-icons type="weixin" color="#fff" size="32"></uni-icons> -->
				<!-- 微信授权(注册)登录 -->
				登录
			</view>
			<view class="footer">
				<!-- <label class="radio">
					<radio  @click="changeRaido" value="1" :checked="raido" />
				</label> -->
				<view>
					注册登录即同意
					<text class="jump" @click="jump(4)">《用户协议》</text>
					<text class="jump" @click="jump(5)">《隐私声明》</text> 
				</view>
			</view>
		</view>
	</view>
</template>
<!-- <script src="http://res.wx.qq.com/open/js/jweixin-1.6.0.js"/> -->
<script>
	import topTitle from "@/components/topTitle.vue"
	export default {
		components:{
			topTitle
		},
		data() {
			return {
				appId:'wx9dfdca82dff9df74',
				logo:"http://nydjoss.oss-cn-zhangjiakou.aliyuncs.com/hxdj/4f/8c3f8e5a3b8ea4730e1c9751b25d3b.png",
				raido:true,
				jumpUrl:'',
			}; 
		},
        onLoad(options) {
			// uni.navigateTo({
			// 	url:'/pages/login/login'
			// })
			this.getConfig()
			let spread_user_id = uni.getStorageSync('spread_user_id');
			let channel_id = uni.getStorageSync('channel_id');
			var that = this
			
            let code = this.getQueryString("code");
			console.log(code,'---------------------code')
    //        if(code == null && navigator.userAgent.toLowerCase().indexOf('micromessenger') !== -1){
				// this.login()
    //        }
	
			if (code != null) {
                let params = {
                     code: code,
                };
				// uni.getLocation({
				// 	type: 'gcj02',
				// 	isHighAccuracy:true,
				// 	highAccuracyExpireTime:1000,
				// 	success: (res) => {
				// 		console.log(res, '----------------------获取地址res')
				// 		//这里的代码
				// 	}
				// })
				that.$http({
				    url:'/api/wechat/auth',
				    method:'post',
				    data:{
				       code,
					   spread_user_id,
					   channel_id,
					   // latitude:res.latitude,
					   // longitude:res.longitude,
				    },
				    success:result=>{
				        if(result.code == 200){
				            // uni.setStorageSync('token',result.data.token)
				            uni.showToast({
				                title:'授权登录成功'
				            })
							setTimeout(function() {
								uni.hideLoading()
							}, 1500);
							that.getUserInfo(result.data.token)
				        }
				    }
				})
                
            }
        },
		methods:{
			//跳转协议
			jump(i){
				uni.navigateTo({
					url:`/pages/my/agreement/agreement?infoId=${i}`
				})
			},
			//是否同意隐私政策
			changeRaido(){
				this.raido = !this.raido
			},
			login() {
				uni.showLoading({
					title:'登录中...'
				})
				if(!this.raido){
					uni.showToast({
						title:'请查看并同意用户协议与隐私政策',
						icon:"none"
					})
					return false;
				}
                let redirect_uri = encodeURIComponent(window.location.href)
                window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${this.appId}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect`;
			},
			getQueryString(name) {
				//code截取
				var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
				var r = window.location.search.substr(1).match(reg);
				if (r != null) {
			        sessionStorage.setItem("code", unescape(r[2]));
			        return unescape(r[2]);
			    } else {
			        return null;
			    }
			},
			getConfig(){
				this.$http({
					url:'/api/config',
					method:'get',
					success:result=>{
						console.log(result)
						this.logo = result.data.user_logo
						this.appId = result.data.wechat_appid
						this.jumpUrl = result.data.site_url
						this.$forceUpdate()
					}
				})
			},
			//获取商家logo
			getLogo(){
				
				uni.showLoading({
					title:'加载中...'
				})
				this.$http({
					url:'/api/sys_config/site_user_avatar',
					method:'get',
					success:result=>{
						console.log(result,'-----123')
						if(result.code == 200){
							this.logo = result.data.content
							uni.hideLoading()
						}
					}
				})
			},
			//获取用户信息
			getUserInfo(token){
				this.$http({
					url:'/api/user/info',
					method:'get',
					header: {
						token:token
					},
					success:result=>{
						console.log(result,'----------------123')
						if(result.code == 200){
							let userInfo = result.data.info;
							let url = "";
							if(userInfo.bind_phone == 1){
								url = '/pages/index/index'
								uni.setStorageSync('token',token)
								uni.removeStorageSync('spread_user_id')
								uni.removeStorageSync('channel_id')
								if(uni.getStorageSync('share_url')){
									window.location.href= uni.getStorageSync('share_url')
									uni.removeStorageSync('share_url')
								}else{
									if(uni.getStorageSync('order_url')){
										window.location.href=uni.getStorageSync('order_url')
									}else{
										// #ifdef H5
										window.location.href= this.jumpUrl +'/#/pages/index/index'
										// #endif
										// #ifdef APP-PLUS
										uni.switchTab({
											url:'/pages/index/index'
										})
										// #endif
									}
								}
							}else{
								url = `/pages/my/member/bindingTel?token=${token}`;
								uni.reLaunch({
								    url:url
								})
							}
							
						}
					}
				})
			}
		},
	}
</script>

<style lang="scss">
	page{
		width: 100%;
		height: 100%;
		background-color: #fff;
	}
	.loginn{
		background-image: linear-gradient(#e5fff7, #fff);
	}
	.login{
		width: 100%;
		height: 100%;
		
		.top_logo{
			width: 120px;
			height: 120px;
			margin: 150px auto 100px auto;
			margin-bottom: 30px;
			>image{
				width: 100%;
				height: 100%;
			}
		}
		.title{
			text-align: center;
			font-weight: bold;
		}
		.login_button{
			margin: 50px 80px;
			text-align: center;
		    height: 50px;
		    line-height: 50px;
		    background-color: #26c876;	
			color: #fff;
			border-radius: 25px;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 16px;
		}
		.footer{
			width: 100%;
			position: fixed;
			bottom: 60upx;
			display: flex;
			align-items: center;
			justify-content: center;
			line-height: 28.6upx;
			font-size: 28upx;
			// color:;
			.jump{
				color: #26c876;
			}
		}
	}
</style>
